import os

import jieba
import matplotlib.pyplot as plt
import wordcloud
import string

text = []
base_path = r'D:\code\python-big-work\page'
stop_words = set(["的", "地", "已经"]) # 设置过滤词
for i in range(1,8):
    filename = os.path.join(base_path, f'{i}.txt')
    with open(filename, 'r', encoding='utf-8') as f:
        text.append("")
        for line in f.readlines():
            line = line.strip("\n")
            text[i-1] += " ".join(jieba.cut(line))
        text[i-1] = " ".join([word for word in text[i-1].split() if word not in stop_words and len(word) > 1])
        # 生成词云对象
        w = wordcloud.WordCloud(
            background_color="white",  # 设置背景颜色
            font_path=r"C:\Windows\Fonts\simhei.ttf",  # 中文黑体
            width=1800,
            height=1600)

        w.generate(text[i-1])  # 向词云对象中加载文本
        w.to_file(os.path.join("D:\\code\\python-big-work\\img", f'NO_{i}.png'))


        words = jieba.lcut(text[i-1])
        # 构建词频字典:词作为“键”，词出现的次数作为“值”

        word_freq = {}
        for word in words:
            if len(word) == 1:  # 排除标点符号及“的、地、得”等词
                continue
            else:
                # 建立“键:值”对
                word_freq[word] = word_freq.get(word, 0) + 1
        items = list(word_freq.items())
        items.sort(key=lambda x: x[1], reverse=True)  # 排序
        word_text = ""
        max_number = 10
        for j in range(max_number):
            word, freq = items[j]
            # 把word和freq写入word_text变量中
            word_text += "{}: {}\n".format(word, freq)

        wt = wordcloud.WordCloud(
            background_color="black",  # 设置背景颜色
            font_path=r"C:\Windows\Fonts\simsun.ttc",  # 中文黑体
            width=1800,
            height=1600)

        wt.generate(word_text)  # 向词云对象中加载文本

        wt.to_file(os.path.join("D:\\code\\python-big-work\\img", f'NO_{i}_wordfreq.png'))








